/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.client.view;

import javax.swing.JOptionPane;
import com.agenda.client.ClienteServidor;
import com.agenda.client.requisicoes.CadastrarUsuarioRequisicao;
import com.agenda.model.RespostaRequisicao;
import com.agenda.model.Usuario;

/**
 *
 * @author Murilo
 */
public class CadastrarDialogView extends javax.swing.JDialog {

	/**
	 * Creates new form CadastrarDialogView
	 */
	public CadastrarDialogView(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of
	 * this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		emailText = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		senhaPass = new javax.swing.JPasswordField();
		jLabel3 = new javax.swing.JLabel();
		conformarSenhaPass = new javax.swing.JPasswordField();
		cadastrarButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Cadastrar Usuário");
		setResizable(false);

		jLabel1.setText("email");

		jLabel2.setText("senha");

		jLabel3.setText("confirmação senha");

		cadastrarButton.setText("Cadastrar");
		cadastrarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cadastrarButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			layout.createSequentialGroup().addContainerGap().addGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addGroup(
					layout.createSequentialGroup().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel2).addComponent(jLabel1))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
							layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(emailText).addComponent(
								senhaPass, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))).addGroup(
					layout.createSequentialGroup().addComponent(jLabel3).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
							layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(cadastrarButton).addComponent(
								conformarSenhaPass, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))))
				.addContainerGap(28, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			layout.createSequentialGroup().addGap(42, 42, 42).addGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(emailText,
					javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(
					layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(senhaPass,
						javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(
					layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3).addComponent(conformarSenhaPass,
						javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(34, 34, 34).addComponent(cadastrarButton).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 301) / 2, (screenSize.height - 230) / 2, 301, 230);
	}// </editor-fold>//GEN-END:initComponents

	private void cadastrarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastrarButtonActionPerformed
		cadastrarUsuario();
	}//GEN-LAST:event_cadastrarButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton cadastrarButton;
	private javax.swing.JPasswordField conformarSenhaPass;
	private javax.swing.JTextField emailText;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPasswordField senhaPass;
	// End of variables declaration//GEN-END:variables

	private ClienteServidor clienteServidor;

	public void setClienteServidor(ClienteServidor clienteServidor) {
		this.clienteServidor = clienteServidor;
	}

	private void cadastrarUsuario() {

		if (emailText.getText().isEmpty()) {
			showMessage("Informe o email.");
			return;
		}
		if (senhaPass.getText().isEmpty()) {
			showMessage("Informe uma senha.");
			return;
		}
		if (!senhaPass.getText().equals(conformarSenhaPass.getText())) {
			showMessage("A senhas informadas são diferentes.");
			return;
		}

		Usuario usuario = new Usuario();
		usuario.setLoginEmail(emailText.getText());
		usuario.setSenha(senhaPass.getText());
		CadastrarUsuarioRequisicao cadadastrarReq = new CadastrarUsuarioRequisicao();
		cadadastrarReq.setUsuario(usuario);

		try {
			RespostaRequisicao respostaRequisicao = clienteServidor.enviarInformacao(cadadastrarReq);

			if (respostaRequisicao.isErro())
				showMessage(respostaRequisicao.getMsg());
			else
				showMessage("Usuário cadastrado com sucesso.");

		} catch (Exception e) {
			showMessage("Erro ao comunicar com o servidor. " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			ClienteServidor.getINSTANCIA().liberarComunicacao();
		}
	}

	private void showMessage(String msg) {
		JOptionPane.showMessageDialog(this, msg);
	}
}
